#include "Background.hpp"

Background::Background(RessourceManager* ressourceManagerPtr) : m_ressourceManager(ressourceManagerPtr)
{

}

void Background::Render(sf::RenderTarget& App) const
{
	for(int i = 0; i < m_listRessource.size(); i++)
		App.Draw(*(m_listRessource.at(i).second));
}

bool Background::Load(std::string filename, BackgroundType type)
{
	// if filename is null, return false
	if(filename == "")
		return false;

	ImageRessource* _ressourceImage = m_ressourceManager->AddImage("UNKNOW_NAME_" + filename, filename);

	// if ressourceImage point to nothing
	if(_ressourceImage == NULL)
		return false;


	if(_ressourceImage->IsLoaded())
	{
	    _ressourceImage->Ressource()->SetSmooth(false);

		// New sprite
		sf::Sprite* spritePtr = new sf::Sprite;

		spritePtr->SetImage(*(_ressourceImage->Ressource()));
		spritePtr->SetPosition(0, 0);

		//Set size map
		m_size.x += static_cast<int>(spritePtr->GetSize().x);
		m_size.y = static_cast<int>(spritePtr->GetSize().y);

		// Add into list
		m_listRessource.push_back(std::pair<ImageRessource*, sf::Sprite*>(_ressourceImage, spritePtr));

		_ressourceImage = NULL;
		spritePtr = NULL;
	}
	else
	{
		_ressourceImage = NULL;
		m_ressourceManager->UnloadImage("UNKNOW_NAME_" + filename);

		// Set Argument
		std::string argExt("dkcrCut.exe " + filename);
		std::stringstream out;

		// Call extern program
		system(argExt.c_str());

		// Variable for the loop
		bool running = true;
		int i = 1;

		std::string pathImage = filename.substr(0, (filename.size() - 4 ));

		while(running)
		{
			out.str("");
			out << i;

			std::string textImage = pathImage + "_" + out.str() + ".png";

			// Add an image into Ressource Manage
			_ressourceImage = m_ressourceManager->AddImage("UNKNOW_NAME_" + textImage, textImage);

			if(_ressourceImage->IsLoaded())
			{
				sf::Sprite* spritePtr = new sf::Sprite;
				spritePtr->SetImage(*(_ressourceImage->Ressource()));

				// Remove Smooth
                _ressourceImage->Ressource()->SetSmooth(false);

				// Add into list
				m_listRessource.push_back(std::pair<ImageRessource*, sf::Sprite*>(_ressourceImage, spritePtr));

				// Set Size Map
				m_size.x += static_cast<int>(spritePtr->GetSize().x);
				m_size.y = static_cast<int>(spritePtr->GetSize().y);

			   if(i == 1)
				spritePtr->SetPosition(0, 0);
			   else
				   spritePtr->SetPosition((m_listRessource.at(i - 2).second->GetPosition().x + m_listRessource.at(i - 2).second->GetSize().x), 0);


				spritePtr = NULL;
				_ressourceImage = NULL;
			}
			else
			{
				_ressourceImage = NULL;
				// UnLoad Image if it's  not exist
				m_ressourceManager->UnloadImage("UNKNOW_NAME_" + textImage);
				running = false;
			}
		i++;
		}
	}
}

sf::Vector2i Background::Size()
{
    return m_size;
}

BackgroundType Background::Type()
{
    return m_type;
}

void Background::Scrolling(int view_X,int Carte_X)
{
    int move_x = ((view_X * m_size.x)/Carte_X);
    for(int i = 0;i < m_listRessource.size();i++)
    {
        if(i = 0)
            m_listRessource[i].second->SetPosition(move_x,0);
        else
            m_listRessource[i].second->SetPosition((move_x + (i * m_listRessource[0].second->GetSize().x)),0);
    }
}
